﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NailManager.DTO;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace NailManager.DAO
{
    public class PromotionDAO : DataProvider
    {
        public ArrayList getList()
        {
            ArrayList arr;
            try
            {
                string query = "SELECT * FROM PROMOTION";
                adapter = new OleDbDataAdapter(query, connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                arr = ConvertDataSetToArrayList(dataset);

            }
            catch (Exception ex)
            {
                arr = new ArrayList();
            }
            return arr;
        }

        public ArrayList getList(string condition)
        {
            ArrayList arr;
            try
            {
                string query = "SELECT * FROM PROMOTION" + condition;
                adapter = new OleDbDataAdapter(query, connection);
                DataSet dataset = new DataSet();
                adapter.Fill(dataset);
                arr = ConvertDataSetToArrayList(dataset);

            }
            catch (Exception ex)
            {
                arr = new ArrayList();
            }
            return arr;
        }

        public void insert(Promotion info)
        {
            try
            {
                string insertCommand = "";
                OleDbParameter parameter = new OleDbParameter("@DiscountPercent", OleDbType.Double);
                parameter.Value = info.DiscountPercent;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@EndDate", OleDbType.DBDate);
                parameter.Value = info.EndDate;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@StartDate", OleDbType.DBDate);
                parameter.Value = info.StartDate;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@Title", OleDbType.VarChar);
                parameter.Value = info.Title;
                command.Parameters.Add(parameter);

                executeNonQuery(insertCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        public void update(Promotion info)
        {
            try
            {
                string updateCommand = "";
                OleDbParameter parameter = new OleDbParameter("@PromotionID", OleDbType.Integer);
                parameter.Value = info.PromotionID;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@DiscountPercent", OleDbType.Double);
                parameter.Value = info.DiscountPercent;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@EndDate", OleDbType.DBDate);
                parameter.Value = info.EndDate;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@StartDate", OleDbType.DBDate);
                parameter.Value = info.StartDate;
                command.Parameters.Add(parameter);

                parameter = new OleDbParameter("@Title", OleDbType.VarChar);
                parameter.Value = info.Title;
                command.Parameters.Add(parameter);

                executeNonQuery(updateCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        public void delete(int id)
        {
            try
            {
                string deleteCommand = "";
                OleDbParameter parameter = new OleDbParameter("@PromotionID", OleDbType.Integer);
                parameter.Value = id;
                command.Parameters.Add(parameter);

                executeNonQuery(deleteCommand);
                command.Parameters.Clear();
            }
            catch (Exception ex)
            {
                command.Parameters.Clear();
                return;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            Promotion info = new Promotion();
            info.PromotionID = int.Parse(dt.Rows[i]["PromotionID"].ToString());
            info.Title = dt.Rows[i]["Title"].ToString();
            info.StartDate = DateTime.Parse(dt.Rows[i]["StartDate"].ToString());
            info.EndDate = DateTime.Parse(dt.Rows[i]["EndDate"].ToString());
            info.DiscountPercent = float.Parse(dt.Rows[i]["DiscountPercent"].ToString());

            return (object)info;
        }
    }
}
